//
//  RegistStepTwoView.swift
//  cxd4iphone
//
//  Created by hexy on 12/8/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit
import ReactiveCocoa

class RegistView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func makeUI() {
        
        let wrap = UIView()
        
        addSubview(wrap)
        wrap.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(UIEdgeInsetsMake(15, 20, -15, -20))
        }
        
        wrap.addSubview(userNameTextField)
        userNameTextField.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(wrap)
            make.height.equalTo(xx_height(40))
        }
        
        wrap.addSubview(passwdTextField)
        passwdTextField.snp_makeConstraints { (make) -> Void in
            
            make.left.right.height.equalTo(userNameTextField)
            make.top.equalTo(userNameTextField.snp_bottom).offset(15)
        }
        
        wrap.addSubview(refferPhoneTextField)
        refferPhoneTextField.snp_makeConstraints { (make) -> Void in
            
            make.left.right.height.equalTo(passwdTextField)
            make.top.equalTo(passwdTextField.snp_bottom).offset(15)
        }

        wrap.addSubview(registBtn)
        registBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(refferPhoneTextField.snp_bottom).offset(xx_height(20))
            make.left.right.equalTo(wrap)
            make.height.equalTo(xx_height(40))
        }
        
        wrap.addSubview(agreeBtn)
        agreeBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(registBtn.snp_bottom).offset(xx_height(5))
            make.right.equalTo(registBtn)
        }
    }
    
    lazy var userNameTextField = UITextField(placeholder: "输入用户名(字母和数字的组合)", leftImage: UIImage(named: "auth_username")!, keyboardType:.Default)
    lazy var passwdTextField = UITextField(placeholder: "请输入密码(最少6位)")
    lazy var refferPhoneTextField = UITextField(placeholder: "邀请人手机号码(可选)", leftImage: UIImage(named: "auth_tele")!, keyboardType:.NumberPad)

    
    lazy var registBtn: UIButton = {
        
        let btn = UIButton(title: "注 册", titleColor: UIColor.whiteColor(), titleSize: 18, bgColor: Define.blueColor)
        
        btn.setBackgroundImage(UIImage.createImgaeWithColor(Define.grayColor), forState: .Disabled)
        
        RACSignal.combineLatest([self.userNameTextField.rac_textSignal(), self.passwdTextField.rac_textSignal()]).map { (tuple) -> AnyObject! in
            
            return self.isValid(tuple as! RACTuple)
            
            }.subscribeNext { (enable) -> Void in
                
                btn.enabled =  (enable as! Bool)
        }
        
        return btn
    }()
    
    lazy var  agreeBtn: UIButton = {
        
        let btn = UIButton(title: " <<同意注册协议>>", normalImage: UIImage(named: "withdraw_normal")!, selectImage: UIImage(named: "withdraw_selected")!)
        btn.titleLabel?.font = UIFont.systemFontOfSize(10)
        btn.selected = true
        return btn
    }()
    
    func isValid(tuple: RACTuple) -> Bool {
        
        let userName = tuple.first as! NSString
        let pwd = tuple.second as! NSString
        
        if userName.length > 0 && pwd.length > 0 {
            
//            if newPwd != surePwd {
//                
//                return false
//            }
            return true
        }
        return false
    }
}
